//
//  CacheTool.m
//  Car
//
//  Created by 何晓文 on 15/10/5.
//  Copyright © 2015年 朱鸿儒. All rights reserved.
//

#import "CacheTool.h"

@implementation CacheTool
//计算单个文件大小
+(float)fileSizeAtPath:(NSString *)path{
	NSFileManager *fileManager=[NSFileManager defaultManager];
	if([fileManager fileExistsAtPath:path]){
		long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
		return size/1024.0/1024.0;
	}
	return 0;
}
//计算目录大小
+(float)folderSizeAtPath:(NSString *)path{
	NSFileManager *fileManager=[NSFileManager defaultManager];
	NSLog(@"%@",path);
	float folderSize = 0;
	if ([fileManager fileExistsAtPath:path]) {
		//遍历所有的文件路径
		NSArray *childerFiles=[fileManager subpathsAtPath:path];
		for (NSString *fileName in childerFiles) {
			NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
			folderSize +=[CacheTool fileSizeAtPath:absolutePath];
		}
		//SDWebImage框架自身计算缓存的实现
		folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
		return folderSize;
	}
	return 0;
}
//删除缓存
+(void)clearCache:(NSString *)path{
	NSFileManager *fileManager=[NSFileManager defaultManager];
	if ([fileManager fileExistsAtPath:path]) {
		NSArray *childerFiles=[fileManager subpathsAtPath:path];
		for (NSString *fileName in childerFiles) {
			
			NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
			[fileManager removeItemAtPath:absolutePath error:nil];
		}
	}
	[[SDImageCache sharedImageCache] cleanDisk];
}
@end
